﻿using System;
using System.Collections.Generic;
using System.Text;
using QuanLyKhachSan.DTO;
using System.Data;

namespace QuanLyKhachSan.DAO
{
    public class NguoiDungDAO
    {
        // Them nguoi dung
        public static bool Insert(NguoiDungDTO nguoiDungdto)
        {
            string sql = "INSERT INTO NguoiDung VALUES (N'" + nguoiDungdto.TenDangNhap + "',N'" + nguoiDungdto.MatKhau +"',N'" + nguoiDungdto.HoNguoiDung + "',N'"+ nguoiDungdto.TenLot + "',N'" + nguoiDungdto.TenNguoiDung + "'," + nguoiDungdto.LoaiNguoiDung + ",'" + nguoiDungdto.CMND1 + "',N'" +nguoiDungdto.DiaChi +"')";
            DataAccessHelper s = new SqlDataAccessHelper();

            int kq = s.ExecuteNonQuery1(sql);
            if (kq < 0)
                return false;
            return true;
        }

        // Xoa nguoi dung
        public static bool Delete(string tenDangNhap)
        {
            string sql = "DELETE from NguoiDung where tenDangNhap=N'" +tenDangNhap + "'" ;
            DataAccessHelper s = new SqlDataAccessHelper();
            int kq = s.ExecuteNonQuery1(sql);
            if (kq < 0)
                return false;
            return true;
        }

        // Cap nhat thong tin nguoi dung
        public static bool Update(NguoiDungDTO nguoiDungdto)
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", nguoiDungdto.TenDangNhap);
            para.Add("matKhau", nguoiDungdto.MatKhau);
            para.Add("ten", nguoiDungdto.TenNguoiDung);
            para.Add("@ho", nguoiDungdto.HoNguoiDung);
            para.Add("@tenDem", nguoiDungdto.TenLot);
            para.Add("@cmnd", nguoiDungdto.CMND1);
            para.Add("@diaChi", nguoiDungdto.DiaChi);
            DataAccessHelper help = new SqlDataAccessHelper();
            int kq = help.ExecuteNonProcedure("sp_UpdateNguoiDung", para);
            if (kq == 0)
            {
                return false;
            }
            return true;
        }
        
        // Lay danh sach nguoi dung
        public static DataTable LayDanhSachNguoiDung()
        {
            DataTable dt = new DataTable();
            string sql = "Select * from NguoiDung";
            DataAccessHelper s = new SqlDataAccessHelper();
            dt = s.ExecuteQuery(sql);
            return dt;
        }
        public static DataTable LayDSNguoiDungTheoTenND(string tenNguoiDung)
        {
            DataTable dt = new DataTable();

            string sql = "Select * from NguoiDung where tenNguoiDung like N'%" + tenNguoiDung + "%' ";
            DataAccessHelper s = new SqlDataAccessHelper();
            dt = s.ExecuteQuery(sql);
            return dt;
        }
        
        //Lay thong tin nguoi dung theo UserName, pass
        public static NguoiDungDTO ThongTinNguoiDung(string userName, string pass)
        {
            DataTable dt = new DataTable(); 

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", userName);
            para.Add("matKhau", pass);
            DataAccessHelper help = new SqlDataAccessHelper();
            dt = help.ExecuteProcedure("sp_KiemTraLogin", para);

            NguoiDungDTO nd = new NguoiDungDTO();
            if (dt.Rows.Count > 0)
            {
                 nd = new NguoiDungDTO(dt);
            }
            
            
            return nd;
        }
    }
}
